package com.education.wisdom.service.impl;


import com.education.common.utils.Convert;
import com.education.common.utils.StringUtils;
import com.education.common.utils.UUIDUtils;
import com.education.wisdom.mapper.CatalogPaperMapper;
import com.education.wisdom.mapper.PaperMapper;
import com.education.wisdom.mapper.PaperQuestionMapper;
import com.education.wisdom.mapper.QuestionsMapper;
import com.education.wisdom.pojo.*;
import com.education.wisdom.service.IAnswersService;
import com.education.wisdom.service.IPaperService;
import com.education.wisdom.service.JrAnswTwoService;
import com.education.wisdom.vo.PaperVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 试卷 服务层实现
 *
 * @author ruoyi
 * @date 2018-11-13
 */
@Service
public class PaperServiceImpl implements IPaperService {
    @Autowired
    private PaperMapper paperMapper;
    @Autowired
    private CatalogPaperMapper catalogPaperMapper;
    @Autowired
    private PaperQuestionMapper paperQuestionMapper;
    @Autowired
    private QuestionsMapper questionMapper;

    @Autowired
    private JrAnswTwoService answTwoService;

    @Autowired
    private IAnswersService answersService;

    /**
     * 查询试卷信息
     *
     * @param id 试卷ID
     * @return 试卷信息
     */
    @Override
    public Paper selectPaperById(Integer id) {
        return paperMapper.selectPaperById(id);
    }

    /**
     * 查询所有试卷
     *
     * @param map
     * @return
     */
    @Override
    public List<Paper> selectAllPaper(Map<String, Object> map) {
        return paperMapper.selectAllPaper(map);
    }

    /**
     * 统计试卷下问题数量
     *
     * @param id
     * @return
     */
    @Override
    public Integer countQuestion(Integer id) {
        return paperMapper.countQuestion(id);
    }

    /**
     * 查询小节下试卷
     *
     * @param catalogId
     * @return
     */
    @Override
    public List<Paper> selectPaperByCatalog(String catalogId) {
        return paperMapper.selectPaperByCatalog(catalogId);
    }

    /**
     * 查询试卷列表
     *
     * @param paper 试卷信息
     * @return 试卷集合
     */
    @Override
    public List<Paper> selectPaperList(Map<String, Object> map) {
        return paperMapper.selectPaperList(map);
    }

    @Override
    public int insertPaperOne(Paper paper) {
        return paperMapper.insertPaper(paper);
    }

    @Override
    public int insertPaper(PaperVo paper) {
        String[] questionIds = paper.getQuestionIds();
        BigDecimal scoure = new BigDecimal(0);
        if (questionIds.length > 0) {
            for (String questionId : questionIds) {
                Questions questions = questionMapper.selectQuestionsById2(Integer.parseInt(questionId));
                scoure = scoure.add(questions.getStScore());
            }
        }
        //添加试卷
        paper.setSjId(UUIDUtils.getByUUId());
        paper.setCreateTime(new Date());
        paper.setSjScore(scoure);
        paperMapper.insertPaper(paper);

        CatalogPaper catalogPaper = new CatalogPaper();
        catalogPaper.setCatalogId(paper.getCatalogId());
        catalogPaper.setPaperId(paper.getId());
        insertPaperQuestion(paper);
        return catalogPaperMapper.insertCatalogPaper(catalogPaper);
    }

    @Override
    public int insertPaperTest(PaperVo paper) {
        BigDecimal scoure = new BigDecimal(0);
//        if (questionIds.length > 0) {
//            for (Integer questionId : questionIds) {
//                Questions questions = questionMapper.selectQuestionsById2(questionId);
//                scoure = scoure.add(questions.getStScore());
//            }
//        }
        paper.setSjId(UUIDUtils.getByUUId());
        paper.setCreateTime(new Date());
        //paper.setSjScore(scoure);
        paperMapper.insertPaper(paper);
        CatalogPaper catalogPaper = new CatalogPaper();
        catalogPaper.setCatalogId(paper.getCatalogId());
        catalogPaper.setPaperId(paper.getId());
        insertPaperQuestion(paper);
        return 1;
    }


    @Override
    @Transactional
    public int updatePaperTest(PaperVo paper) {
        paperMapper.updatePaper(paper);
        paperQuestionMapper.deleteByPaperId(paper.getId());
        paper.setSjId(UUIDUtils.getByUUId());
        paper.setCreateTime(new Date());
        insertPaperQuestion(paper);
        return 1;
    }


//	/**
//     * 新增试卷
//     *
//     * @param paper 试卷信息
//     * @return 结果
//     */
//	@Override
//	public int insertPaper(Paper paper)
//	{
//	    return paperMapper.insertPaper(paper);
//	}

    /**
     * 修改试卷
     *
     * @param paper 试卷信息
     * @return 结果
     */
    @Override
    public int updatePaper(PaperVo paperVo) {
        String[] questionIds = paperVo.getQuestionIds();
        Integer paperId = paperVo.getId();
        BigDecimal scoure = new BigDecimal(0);
        paperQuestionMapper.deleteByPaperId(paperId);
        if (questionIds.length > 0) {
            for (String questionId : questionIds) {
                Questions questions = questionMapper.selectQuestionsById(questionId);
                if (questions!=null) {
                    scoure = scoure.add(questions.getStScore());
                }
            }
            insertPaperQuestion(paperVo);
        }
        paperVo.setSjScore(scoure);
        CatalogPaper catalogPaper = new CatalogPaper();
        catalogPaper.setCatalogId(paperVo.getCatalogId());
        catalogPaper.setPaperId(paperVo.getId());
        catalogPaperMapper.updateCatalogPaper(catalogPaper);
        return paperMapper.updatePaper(paperVo);
    }



    /**
     * 删除试卷对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePaperByIds(String ids) {
        paperQuestionMapper.deletePaperQuestionByPaperId(Convert.toStrArray(ids));
        Answers answers1 = new Answers();
        answers1.setDjPaperid(Integer.valueOf(ids));
        List<Answers> answers = answersService.selectByPaperId2(answers1);
        if (StringUtils.isNotEmpty(answers)){
            for (Answers answer : answers) {
                String[] split = answer.getDjContent().split(",");
                answTwoService.deleteJrAnswTwoByIds(split);
            }
        }
        return paperMapper.deletePaperByIds(Convert.toStrArray(ids));
    }

    @Override
    public List<Paper> selectPaperListById(Map<String, Object> map) {
        return paperMapper.selectPaperListById(map);
    }

    @Override
    public List<Paper> selectPaperAllListById(Map<String, Object> map) {
        return paperMapper.selectPaperAllListById(map);
    }

    @Override
    public List<Paper> selectTitlelist() {
        return paperMapper.selectTitlelist();
    }

    @Override
    public List<Paper> selectTitlelists(Map<String, Object> map) {
        return paperMapper.selectTitlelists(map);
    }

    /**
     * 新增试卷与试题关联信息
     *
     * @param paper 用户对象
     */
    public void insertPaperQuestion(PaperVo paper) {
        List<PaperVo> list = new ArrayList<PaperVo>();
        for (String questionId : paper.getQuestionIds()) {
            PaperQuestion paperQuestion = new PaperQuestion();
            paperQuestion.setPaperId(paper.getId());
            paperQuestion.setQuestionId(Integer.parseInt(questionId));
            paperQuestionMapper.insertPaperQuestion(paperQuestion);
        }

    }

    @Override
    public List<Paper> selectAllForPaper(Map<String, Object> map) {
        return paperMapper.selectAllForPaper(map);
    }

    @Override
    public List<Paper> selectByAppraisalCatalogId(Integer appraisalCatalogId) {
        return paperMapper.selectByAppraisalCatalogId(appraisalCatalogId);
    }

    @Override
    public List<Paper> selectByLike(String name,String appraisalCatalogId) {
        return paperMapper.selectByLike(name,appraisalCatalogId);
    }

    @Override
    public List<Paper> selectChapterTest(String userId) {
        return paperMapper.selectChapterTest(userId);
    }

    @Override
    public List<Paper> selectVocationalSkillTest(String userId) {
        return paperMapper.selectVocationalSkillTest(userId);
    }

    @Override
    public List<Paper> selectVocationalSkillTest1(String userId) {
        return paperMapper.selectVocationalSkillTest1(userId);
    }
}
